using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI.WebControls;
using CodeStory.Core.ObjectEntity;
using CodeStory.Core.Providers;
using Activa.CoolStorage;

namespace CodeStory.Core.UI
{
	public class PortletEditMenu : Menu
	{
		IRefreshable refresh;
		public PortletEditMenu(PagePortletObject pageportletObject,IList<string> paneList,IRefreshable refresh) {
			PagePortletID = pageportletObject.PagePortletID;
			this.refresh = refresh;
			CreateMenuItems(pageportletObject, paneList);
		}

		private void CreateMenuItems(PagePortletObject pageportletObject, IList<string> paneList) {
			MenuItem root = new MenuItem("Root", "", "", "####");
			Items.Add(root);
			MenuItem delete = new MenuItem("Delete", "delete");
			root.ChildItems.Add(delete);
			MenuItem move = new MenuItem("Move", "", "", "####");
			root.ChildItems.Add(move);
			string paneName = pageportletObject.PaneName;
			foreach (string str in paneList) {
				if (string.Equals(str, paneName, StringComparison.OrdinalIgnoreCase)) continue;
				string text = string.Format("Move to {0}", str);
				MenuItem item = new MenuItem(text, "move:" + str);
				move.ChildItems.Add(item);
			}
			MenuItem setting = new MenuItem("Setting","","","~/CodeStory_Core_Admin_PortletSetting.portlet.aspx?pageportletid=" + pageportletObject.PagePortletID);
			root.ChildItems.Add(setting);
			if (pageportletObject.InsertOrder > 0) {
				MenuItem before = new MenuItem("Move before", "order:before");
				root.ChildItems.Add(before);
			}

			CSList<PagePortletObject> list = PagePortletObject.List("PageID=@PageID and PaneName=@PaneName", "@PageID", pageportletObject.PageID, "@PaneName", pageportletObject.PaneName).OrderedBy("InsertOrder");
			if (pageportletObject.InsertOrder < list.Count-1) {
				MenuItem after = new MenuItem("Move after", "order:after");
				root.ChildItems.Add(after);
			}
		}
		public Guid PagePortletID {
			get { 
				object obj = ViewState["PagePortletID"];
				return obj == null ? Guid.Empty : (Guid)obj;
			}
			set { ViewState["PagePortletID"] = value; }
		}

		protected override void OnMenuItemClick(MenuEventArgs e) {
			base.OnMenuItemClick(e);
			string v = e.Item.Value;
			if (string.IsNullOrEmpty(v)) return;
			string[] sv = v.Split(':');
			string command = "", arg = "";
			if (sv.Length > 1) {
				command = sv[0];
				arg = sv[1];
			} else {
				command = v;
			}
			bool flag = false;
			command = command.ToLower();
			switch (command) {
				case "delete":
					flag = DeletePortlet();
					break;
				case "move":
					flag = MovePortlet(arg);
					break;
				case "order":
					flag = OrderPortlet(arg);
					break;
				default:
					break;
			}
			if (flag && refresh != null) {
				refresh.Refresh();
			}
		}

		private bool OrderPortlet(string arg) {
			PagePortletObject obj = PagePortletObject.Read(PagePortletID);
			CSList<PagePortletObject> list = PagePortletObject.List("PageID=@PageID and PaneName=@PaneName", "@PageID", obj.PageID, "@PaneName",obj.PaneName).OrderedBy("InsertOrder");
			int index = -1;
			for (int i = 0; i < list.Count; i++) {
				if (list[i].PagePortletID.Equals(PagePortletID)) {
					index = i;
					break;
				}
			}
			list.RemoveAt(index);
			if (arg.Equals("after")) {
				list.Insert(index + 1,obj);
			} else {
				list.Insert(index - 1,obj);
			}
			for (int i = 0; i < list.Count; i++) {
				list[i].InsertOrder = i;
				list[i].Save();
			}

			return true;
		}

		private bool MovePortlet(string paneName) {
			PagePortletObject obj = PagePortletObject.Read(PagePortletID);
			obj.PaneName = paneName;
			obj.Save();
			return true;
		}

		private bool DeletePortlet() {
			PagePortletObject obj = PagePortletObject.Read(PagePortletID);
			obj.Delete();
			return true;
		}
	}
}
